package com.wocao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wocao.entity.*;
import com.wocao.service.ZyOwnerRoomRecordService;
import com.wocao.service.ZyOwnerRoomService;
import com.wocao.service.ZyOwnerService;
import com.wocao.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 业主  前端控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/zyOwner")
public class ZyOwnerController {

    @Autowired
    private ZyOwnerService zyOwnerService;

    @Autowired
    private ZyOwnerRoomService zyOwnerRoomService;

    @Autowired
    private ZyOwnerRoomRecordService zyOwnerRoomRecordService;

    //业主分页
    @PostMapping("owners/{current}")
    public ResponseResult<Page<ZyOwner>> page(@PathVariable int current, @RequestBody ReqBody reqBody){
        Page<ZyOwner> page = zyOwnerService.allPage(new Page<>(current, reqBody.getSize()),reqBody);
        List<ZyOwner> records = page.getRecords();
        for (ZyOwner zyOwner : records) {
            String sex = zyOwner.getOwnerGender();
            if ("Male".equals(sex)){
                zyOwner.setOwnerGender("男");
            }else if ("Female".equals(sex)){
                zyOwner.setOwnerGender("女");
            } else if ("Unknow".equals(sex)) {
                zyOwner.setOwnerGender("未知");
            }
        }
        page.setRecords(records);
        return ResponseResult.ok(page);
    }

    //添加

    //修改

    //解绑
    @GetMapping("/owner/{ownerRoomId}")
    public ResponseResult outOwner(@PathVariable String ownerRoomId, String userName){
        try {
            ZyOwnerRoom ownerRoom = zyOwnerRoomService.getOne(new QueryWrapper<ZyOwnerRoom>().eq("owner_room_id", ownerRoomId));
            ownerRoom.setRoomStatus("3");
            ownerRoom.setUpdateTime(LocalDateTime.now());
            ownerRoom.setUpdateBy(userName);
            zyOwnerRoomService.updateById(ownerRoom);

            ZyOwnerRoomRecord ownerRoomRecord = zyOwnerRoomRecordService.getOne(new QueryWrapper<ZyOwnerRoomRecord>().eq("owner_room_id", ownerRoomId));
            ownerRoomRecord.setRoomStatus("3");
            ownerRoomRecord.setUpdateTime(LocalDateTime.now());
            ownerRoomRecord.setUpdateBy(userName);
            zyOwnerRoomRecordService.updateById(ownerRoomRecord);
            return ResponseResult.ok();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //导出
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<ZyOwner> records = zyOwnerService.allPage(new Page<>(), new ReqBody()).getRecords();
        for (ZyOwner zyOwner : records) {
            String sex = zyOwner.getOwnerGender();
            if ("Male".equals(sex)) {
                zyOwner.setOwnerGender("男");
            } else if ("Female".equals(sex)) {
                zyOwner.setOwnerGender("女");
            } else if ("Unknow".equals(sex)) {
                zyOwner.setOwnerGender("未知");
            }
        }
        ExcelUtil.export(response, ZyOwner.class, records);
    }

    @GetMapping("/findZyOwner")
    public ResponseResult findZyOwner(
            @RequestParam(required = false, defaultValue = "1") Long current,
            @RequestParam(required = false, defaultValue = "5") Long size,
            @RequestParam(required = false, defaultValue = "") Long roomStatus) {
        try {
            IPage<ZyOwner> zyOwnerAll = zyOwnerService.getZyOwnerAll(current, size, roomStatus);
            return ResponseResult.ok(zyOwnerAll);
        } catch (Exception e) {
            return ResponseResult.error(500L, "查询失败");
        }
    }


    @GetMapping("findZyOwnerRoomRecord")
    public ResponseResult findZyOwnerRoomRecord(@RequestParam  String ownerRealName) {
        try {
            List<ZyOwner> ownerRoomRecordAll = zyOwnerService.getOwnerRoomRecordAll(ownerRealName);
            return ResponseResult.ok(ownerRoomRecordAll);
        } catch (Exception e) {
            return ResponseResult.error(500L, "查询失败");
        }
    }

}
